package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.CoinType;
import com.hyk.dto.R;
import com.hyk.service.CoinTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "币种类型的控制器")
@RequestMapping("/coinTypes")
public class CoinTypeController {

    @Resource
    private CoinTypeService coinTypeService;

    @GetMapping
    @ApiOperation("分页查询币种类型")
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<Page<CoinType>> list(@ApiIgnore Page<CoinType> page,String code){
        QueryWrapper<CoinType> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(code)){
            wrapper.eq("code",code);
        }
        wrapper.orderByDesc("last_update_time");
        return R.ok(coinTypeService.page(page,wrapper));
    }

    @PostMapping
    @ApiOperation("新增币种")
    @PreAuthorize("hasAuthority('trade_coin_type_create')")
    public R add(@RequestBody CoinType coinType){
        boolean save = coinTypeService.save(coinType);
        if (save) {
            return R.ok();
        }
        return R.fail();
    }

    @PatchMapping
    @ApiOperation("编辑币种")
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    public R update(@RequestBody CoinType coinType){
        boolean update = coinTypeService.updateById(coinType);
        if (update) {
            return R.ok();
        }
        return R.fail();
    }

    @PostMapping("/setStatus")
    @ApiOperation("修改状态")
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    public R changeStatus(@RequestBody CoinType coinType){
        boolean update = coinTypeService.updateById(coinType);
        if (update) {
            return R.ok();
        }
        return R.fail();
    }

    @GetMapping("/all")
    @ApiOperation("all")
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<List<CoinType>> allList(byte status){
        QueryWrapper<CoinType> wrapper = new QueryWrapper<>();
        wrapper.eq("status",status);
        wrapper.orderByDesc("last_update_time");
        return R.ok(coinTypeService.list(wrapper));
    }

}
